package com.qkl.xiatang.app.web.controller;


import com.qkl.xiatang.app.dto.upload.CommonQueryInDTO;
import com.qkl.xiatang.app.dto.upload.UploadCommonFileWebDTO;
import com.qkl.xiatang.app.dto.upload.UploadFileWebDTO;
import com.qkl.xiatang.app.service.impl.UploadFileService;
import com.qkl.xiatang.app.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class UploadController {

    @Autowired
    private UploadFileService uploadFileService;

    /**
     * 上传图像
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/appImageUpload")
    public Result appImageUpload(HttpServletRequest request, HttpServletResponse response){

        String savePath = UploadUtils.upload(request);
        if (StringUtils.isEmpty(savePath)){
            return  Result.error(ErrorCodeUtils.UPLOAD_FAIL,"上传失败");
        }
        return Result.success(savePath,"上传成功");
    }


    /**
     * 上传查料台视频
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/uploadCheckVideo")
    public Result uploadCheckVideo(HttpServletRequest request, HttpServletResponse response){
        UploadFileDTO fileDTO = UploadUtils.parseFile(request);
        if(null != fileDTO){
            fileDTO.setUploadType(UploadFileDTO.UPLOAD_TYPE_CHECK);
        }
        return ControllerHelper.toOperateResult(uploadFileService.uploadFile(fileDTO));
    }

    /**
     * 提交上传查料台视频
     * @param webDTO
     * @return
     */
    @PostMapping(value = "/uploadFileCheckSubmit")
    public Result uploadFileCheckSubmit(@RequestBody UploadFileWebDTO webDTO){
        webDTO.setUploadType(UploadFileDTO.UPLOAD_TYPE_CHECK);
        return ControllerHelper.toOperateResult(uploadFileService.uploadFileSubmit(webDTO));
    }

    /**
     * 共公文件上传提交
     * @param webDTO
     * @return
     */
    @PostMapping(value = "/openApi/uploadCommonFileSubmit")
    public Result uploadCommonFileSubmit(@RequestBody UploadCommonFileWebDTO webDTO){
        return ControllerHelper.toOperateResult(uploadFileService.uploadCommonFileSubmit(webDTO));
    }

    /**
     * 共公文件删除
     * @param webDTO
     * @return
     */
    @PostMapping(value = "/openApi/deleteCommonFile")
    public Result deleteCommonFile(@RequestBody UploadCommonFileWebDTO webDTO){
        String uploadId = StringUtils.trimToEmpty(webDTO.getUploadId());
        return ControllerHelper.toOperateResult(uploadFileService.deleteCommonFile(uploadId));
    }

    /**
     * 养殖视频查询
     * @param params
     * @return
     */
    @GetMapping("/queryBreedVideo")
    public Result queryBreedVideo(@RequestParam Map<String,String> params) {
        String pageIndex = StringUtils.trimToEmpty(params.get("pageIndex"));
        String pageSize = StringUtils.trimToEmpty(params.get("pageSize"));
        String sort = StringUtils.trimToEmpty(params.get("sort"));
        String startDate = StringUtils.trimToEmpty(params.get("startDate"));
        String endDate = StringUtils.trimToEmpty(params.get("endDate"));

        CommonQueryInDTO webDTO = new CommonQueryInDTO();
        webDTO.setStartDate(startDate);
        webDTO.setEndDate(endDate);
        webDTO.setSort(AppUtils.parseInt(sort));
        webDTO.setPageIndex(AppUtils.parseInt(pageIndex));
        webDTO.setPageSize(AppUtils.parseInt(pageSize));
        return ControllerHelper.toQueryResult(uploadFileService.queryBreedVideo(webDTO));
    }

    /**
     * 查料台过期视频目录
     * @return
     */
    @GetMapping("/openApi/queryExpireCheckDirectory")
    public Result queryExpireCheckDirectory() {
        return ControllerHelper.toQueryResult(uploadFileService.queryExpireCheckDirectory());
    }

}
